import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static String myToString(int[] array) {
        if(array==null) {
            return "null";
        }
        if(array.length==0) {
            return "[]";
        }
        String ret = "[";
        for(int i = 0; i < array.length; i++) {
            ret += array[i];
            //最后一位数不加逗号
            if(i!=array.length-1) {
                ret += ",";
            } else {
                ret += "]";
            }
        }
        return ret;
    }
    //数组拷贝
    public static int[] myCopyOf(int[] source) {
        //拦截异常
        if(source==null || source.length==0) {
            return null;
        }
        int[] ret = new int[source.length];
        for(int i = 0; i < source.length; i++) {
            ret[i] = source[i];
        }
            return ret;
    }
    //对有序数实现二分查找
    public static int find(int[]array,int num) {
        //拦截异常
        if(array==null || array.length==0) {
            return -1;
        }
        int left = 0;
        int right = array.length-1;
        int mid;
        while(left<=right) {
            mid = left + (right-left)/2;
            if(array[mid] > num) {
                right = mid-1;
            } else if(array[mid] < num) {
                left = mid+1;
            } else {
                return mid;
            }
        }
        //走到这里就是没有找到
        return -1;
    }
    public static boolean judge(int[] array) {
        //既然是判断数组是否有序，那么对于无效数组、空数组，统一返回false，不予判断
        if(array==null || array.length==0) {
            return false;
        }
        //当数组中只有一个元素时，默认有序
        if(array.length==1) {
            return true;
        }
        int i = 0;
        while(i< (array.length-1)) {
            if((array[i]-array[i+1])<=0) {
                i++;
            } else {
                return false;
            }
        }
        return true;
    }
    public static void arrange(int[] array) {
        //拦截异常
        if(array==null || array.length==0) {
            return;
        }
        int news = 0;//用作管理新数组
        int traversal = 0;//用作遍历

        for(traversal=0;traversal< array.length;traversal++) {
            if(array[traversal]%2==0) {
                int tmp = array[traversal];
                array[traversal] = array[news];
                array[news] = tmp;
                news++;
            }
            traversal++;
        }

    }

    public static void main(String[] args) {

    }
    public static void main7(String[] args) {
        //交换两个数组
        //给定两个数组，交换数组中的内容
        int[] arrayOne = new int[]{1,2,3,4,5,6};
        int[] arrayTwo = new int[]{7,8,9,10};
        System.out.println("交换前");
        System.out.println("数组一："+Arrays.toString(arrayOne));
        System.out.println("数组二："+Arrays.toString(arrayTwo));

        int[] tmp = arrayOne;
        arrayOne = arrayTwo;
        arrayTwo = tmp;
        tmp = null;

        System.out.println("交换前");
        System.out.println("数组一："+Arrays.toString(arrayOne));
        System.out.println("数组二："+Arrays.toString(arrayTwo));

    }
    public static void main6(String[] args) {
        //给定整型数组, 把所有的偶数放到数组前面, 把所有奇数放到数组后面
        int[] array = new int[]{1,1,11,1,1,1,2};
        arrange(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main5(String[] args) {
        //判断数组是否有序//升序
        int[] array = new int[]{1,2,3,4,5,6};
        if(judge(array)){
            System.out.println("有序");
        } else {
            System.out.println("无序");
        }
    }
    public static void main4(String[] args) {
        Scanner in = new Scanner(System.in);

        //二分查找
        int[] array = new int[]{1,2,3,4,5,6,7,8,9,10};
        System.out.println("请输入您要查找的数字");
        int num = in.nextInt();
        int ret = find(array,num);
        if(ret==-1) {
            System.out.println("该数组中没有您要找的数");
        } else {
            System.out.println("您要找的数字下标为"+ret);
        }
    }
    public static void main3(String[] args) {
        //数组拷贝，对整型数组拷贝
        int[] source = new int[]{1,2,3};
        int[] ret = myCopyOf(source);
        System.out.println(myToString(myCopyOf(source)));
    }
    public static void main2(String[] args) {

        //数组转字符串
        int[] array = new int[]{1,2,3,4,5,6};
        System.out.println(myToString(array));
    }
//    public class Set {
//        public int x;
//        public void func() {
//            System.out.println("方法一");
//        }
//    }
//    public class Test {
//        public int b;
//        public void funcOne() {
//            System.out.println("方法二");
//        }
//    }
//    public class Combination {
//        private Test test;
//        private Set set;
//
//    }
    public static void main1(String[] args) {
        //Combination cmb = new Combination();


       // System.out.println("Hello world!");
    }
}